<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/5/27
 * Time: 12:35
 */
class hj
{
    private $url = 'https://pay.ecpss.com/sslpayment';
    private $no = '29642';
    private $key='T_^RfSAf';
    private $return_url = '';
    private $advice_url = '';

    function __construct()
    {
        global $web_url;
        $web_url='pay.doowal.com';
        $this->return_url = 'http://'.$web_url . '/pay.php';
        $this->advice_url = 'http://'.$web_url . '/respond.php';
    }

    function pay($order_id, $amount)
    {
        $amount=sprintf('%.2f',$amount);
        $data = [
            'MerNo' => $this->no,
            'BillNo' => $order_id,
            'Amount' => $amount,
            'ReturnURL' => $this->return_url,
            'AdviceURL' => $this->advice_url,
            'SingnInfo' => '',
            'orderTime' => time(),
            'defaultBankNmber' => '',
            'Remark' => '',
            'products' => '充值'
        ];
        $SingnInfo=$this->sign($data);
        //$data['SingnInfo']=$SingnInfo;
echo '开始跳转...';
        echo '<form method="post" action="'.$this->url.'" id="payForm">
	<input type="hidden" name="MerNo" value="'.$this->no.'">
	<input type="hidden" name="BillNo" value="'.$order_id.'">
	<input type="hidden" name="Amount" value="'.$amount.'">
	<input type="hidden" name="ReturnURL" value="'.$this->return_url.'">
	<input type="hidden" name="AdviceURL" value="'.$this->advice_url.'">
	<input type="hidden" name="SignInfo" value="'.$SingnInfo.'">
	<input type="hidden" name="orderTime" value="'.date('YmdHis').'">
	<input type="hidden" name="products" value="充值">
</form>

<script type="text/javascript">
    document.getElementById(\'payForm\').submit();
</script>';
        
    }
    function sign($data){
        //echo $data['MerNo'].'&'.$data['BillNo'].'&'.$data['Amount'].'&'.$this->return_url.'&'.$this->key;
        return strtoupper(md5($data['MerNo'].'&'.$data['BillNo'].'&'.$data['Amount'].'&'.$this->return_url.'&'.$this->key));
    }
    function respond(){
        $BillNo=trim($_REQUEST['BillNo']);
        $Amount=trim($_REQUEST['Amount']);
        $Succeed=trim($_REQUEST['Succeed']);
        $Result=trim($_REQUEST['Result']);
        $SignMD5info=trim($_REQUEST['SignMD5info']);
        if($SignMD5info==strtoupper(md5($BillNo.'&'.$Amount.'&'.$Succeed.'&'.$this->key))) {
            if($Succeed==88) {
                return true;
            }else{
                return $Succeed;
            }
        }else{
            return false;
        }
    }
    function error($i){
        $arr=[1=>'银行代码错误',10=>'商户信息不存在',11=>'加密参数有误',13=>'签名验证有误',15=>'该商户未开通',
        16=>'商户通道未开通',24=>'交易流水号重复',25=>'商户交易流水号重复',26=>'参数有空',22=>'交易网址未绑定或未通过'];
        return $arr[$i];
    }
    function vpost($url,$data,$cookie){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        //curl_setopt($curl, CURLOPT_REFERER,'https://www.baidu.com');// 设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }

    function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }
}